home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / recode.lha / recode-3.2.4 / getopt.c < prev    next >
C/C++ Source or Header  |  1991-10-27  |  3KB  |  90 lines

  1. /* ::[[ @(#) getopt.c 1.5 89/07/02 00:18:19 ]]:: */
  2. #ifndef LINT
  3. static char sccsid[]="::[[ @(#) getopt.c 1.5 89/07/02 00:18:19 ]]::";
  4. #endif
  5.  
  6. /*
  7.  * Here's something you've all been waiting for:  the AT&T public domain
  8.  * source for getopt(3).  It is the code which was given out at the 1985
  9.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  10.  * directly from AT&T.  The people there assure me that it is indeed
  11.  * in the public domain.
  12.  *
  13.  * There is no manual page.  That is because the one they gave out at
  14.  * UNIFORUM was slightly different from the current System V Release 2
  15.  * manual page.  The difference apparently involved a note about the
  16.  * famous rules 5 and 6, recommending using white space between an option
  17.  * and its first argument, and not grouping options that have arguments.
  18.  * Getopt itself is currently lenient about both of these things White
  19.  * space is allowed, but not mandatory, and the last option in a group can
  20.  * have an argument.  That particular version of the man page evidently
  21.  * has no official existence, and my source at AT&T did not send a copy.
  22.  * The current SVR2 man page reflects the actual behavor of this getopt.
  23.  * However, I am not about to post a copy of anything licensed by AT&T.
  24.  */
  25.  
  26. /*
  27. Minor modifications by Rahul Dhesi, 1989
  28. Minor modifications by Francois Pinard, 1991
  29. */
  30.  
  31. #include <stdio.h>
  32. #include <string.h>
  33.  
  34. /* Avoid possible compiler warning if we simply redefine NULL or EOF */
  35. #define XNULL   0
  36. #define XEOF (-1)
  37.  
  38. #define ERR(szz,czz) if(opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}
  39.  
  40. int   opterr = 1;
  41. int   optind = 1;
  42. int   optopt;
  43. char  *optarg;
  44.  
  45. int
  46. getopt(argc, argv, opts)
  47. int   argc;
  48. char  **argv, *opts;
  49. {
  50.    static int sp = 1;
  51.    register int c;
  52.    register char *cp;
  53.  
  54.    if(sp == 1)
  55.       if(optind >= argc ||
  56.          argv[optind][0] != '-' || argv[optind][1] == '\0')
  57.          return(XEOF);
  58.       else if(strcmp(argv[optind], "--") == XNULL) {
  59.          optind++;
  60.          return(XEOF);
  61.       }
  62.    optopt = c = argv[optind][sp];
  63.    if(c == ':' || (cp=strchr(opts, c)) == XNULL) {
  64.       ERR(": illegal option -- ", c);
  65.       if(argv[optind][++sp] == '\0') {
  66.          optind++;
  67.          sp = 1;
  68.       }
  69.       return('?');
  70.    }
  71.    if(*++cp == ':') {
  72.       if(argv[optind][sp+1] != '\0')
  73.          optarg = &argv[optind++][sp+1];
  74.       else if(++optind >= argc) {
  75.          ERR(": option requires an argument -- ", c);
  76.          sp = 1;
  77.          return('?');
  78.       } else
  79.          optarg = argv[optind++];
  80.       sp = 1;
  81.    } else {
  82.       if(argv[optind][++sp] == '\0') {
  83.          sp = 1;
  84.          optind++;
  85.       }
  86.       optarg = XNULL;
  87.    }
  88.    return(c);
  89. }
  90.